<?php

namespace common\service;

use common\model\TaskModel;
use common\model\UserCertCategoryModel;
use support\SingletonTrait;

class TaskCapacityService
{

    // 单例工具
    use SingletonTrait;

    /**
     * 任务产能统计
     * @param $param
     * @return array
     */
    public function certCount($param)
    {
        $startTime = strtotime($param['start_date']);
        $endTime = strtotime($param['end_date']);
        $projectId = $param['project_id'] ?? 0;

        $where = [
            [
                [
                    "task.plan_start_time" => ["lt", $startTime],
                    "task.plan_end_time" => ["gt", $startTime],
                ],
                "task.plan_start_time" => ["BETWEEN", [$startTime, $endTime]],
                "_logic" => "or"
            ],
            "task.user_cert_id" => ["GT", 0],
            "_logic" => "and"
        ];
        if ($projectId) {
            $where['task.project_id'] = $projectId;
        }

        $taskList = model(TaskModel::class)
            ->where($where)
            ->select();

        $startDate = date_create($param['start_date']);
        $endDate = date_create($param['end_date']);
        $dateInterval = date_diff($startDate, $endDate);

        $days = $dateInterval->days;
        $startTime = strtotime(date("Y-m-d", $startTime));

        $userCertIdList = array_column($taskList, 'user_cert_id', 'user_cert_id');

        $userCertList = model(UserCertCategoryModel::class)
            ->alias("ucc")
            ->join("user_cert uc ON ucc.id = uc.user_cert_category_id")
            ->where(['uc.id' => ["IN", implode(',', $userCertIdList)]])
            ->field("uc.id,uc.base_man_hour,uc.level,uc.level_category,ucc.name")
            ->order("id ASC")
            ->select();
        // 重新生成 证书id
        $userCertIdList = array_column($userCertList, 'id', 'id');
        $CertCountDataTpl = array_fill_keys($userCertIdList, 0);
        $res = [
            "data" => [],
            "cert_list" => $userCertList
        ];

        for ($dayOffset = 0; $dayOffset <= $days; ++$dayOffset) {
            $currentStartTime = strtotime("+{$dayOffset} days", $startTime);
            $currentEndTime = strtotime("+1 days", $currentStartTime);
            $tmpCertCountDataTpl = $CertCountDataTpl;
            // 筛选出当前时间段的任务数据
            $currentTaskList = array_filter($taskList, function ($item) use ($currentStartTime, $currentEndTime) {
                if ($item['plan_start_time'] > $currentStartTime && $item['plan_start_time'] < $currentEndTime) {
                    return true;
                }
                if ($item['plan_start_time'] < $currentStartTime && $item['plan_end_time'] >= $currentStartTime) {
                    return true;
                }
                return false;
            });
            // 分组统计
            foreach ($currentTaskList as $taskItem) {
                $tmpCertCountDataTpl[$taskItem['user_cert_id']] += 1;
            }
            $res['data'][] = [
                "date" => date("Y-m-d", $currentStartTime),
                'data' => array_values($tmpCertCountDataTpl)
            ];
        }
        return $res;
    }

}